// -*- coding: utf-8 -*- 
/**
 * Project: AlgorithmsLearn
 *
 * @author: yanking
 * Create time: 2022-03-21 21:26
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package com.LeetCodeT.ArraysTrain;

import org.junit.jupiter.api.Test;

public class FindMaxConsecutiveOnes {
    /**
     * 寻找最大连续1的个数
     *
     * @param nums
     * @return
     */
    public int findMaxConsecutiveOnes(int[] nums) {
        int n = nums.length, max = 0, l = 0, r = 0;
        if (n == 1) {
            return nums[0] == 1 ? 1 : 0;
        }
        while (l < n && nums[l] == 0) {
            l++;
        }
        r = l;
        while (r < n) {
            if (nums[r] != 1 || r == n - 1) {
                if (nums[r] != 0) {
                    max = Math.max(max, r - l + 1);
                } else {
                    max = Math.max(max, r - l);
                }
                while (r < n && nums[r] != 1) {
                    r++;
                }
                l = r;
            }
            r++;
        }
        return max;
    }

    @Test
    public void shout() {
        int[] test = {0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0};
        System.out.println(findMaxConsecutiveOnes(test));
    }


    public int findMaxConsecutiveOnesT(int[] nums) {
        int max = 0, t = 0;
        for (int i = 0; i < nums.length; ++i) {
            if (nums[i] == 1) {
                t += 1;
            } else {
                t = 0;
            }
            max = Math.max(max, t);
        }
        return max;
    }
}